import json
import paho.mqtt.publish as publish
import time

# mqtt客户端，向mqtt server发布数据
# client->mosquitto(broker)->server

HOST = '10.90.18.140'
PORT = 1883

mqtt_payload = {
    "devList": [{
        "devId": 102,
        "devNo": "E582-14",
        "varList": [{
            "varName": "D120",
            "varValue": "26.20",
            "readTime": "2022-08-05 11:16:03",
            "varId": 2,
            "isWarn": 0
        }, {
            "varName": "D180",
            "varValue": "431437",
            "readTime": "2022-08-05 11:16:03",
            "varId": 5,
            "isWarn": 0
        }, {
            "varName": "D182",
            "varValue": "25431",
            "readTime": "2022-08-05 11:16:03",
            "varId": 6,
            "isWarn": 0
        }]
    }],
    "cmdId": 103,
    "ver": "0.1.3.1",
    "gwSn": "OG585WLAN20022500226",
    "gwName": "OG585WLAN20022500226",
    "type": 0
}

if __name__ == '__main__':
    payload = f"{json.dumps(mqtt_payload)}".encode(encoding='utf-8')
    client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    # 发布数据
    for i in range(1000):
        publish.single("xgxx/v1/devices", payload, qos=1, hostname=HOST, port=PORT, client_id=client_id,
                       auth={'username': "mosquitto", 'password': "mosquitto"})
        print('pub ok')
        time.sleep(1)


